<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ video.title }} - 播放页</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#165DFF',
                        secondary: '#FF7D00',
                        dark: '#1E293B',
                        light: '#F8FAFC'
                    },
                    fontFamily: {
                        inter: ['Inter', 'sans-serif'],
                    },
                }
            }
        }
    </script>
    
    <style type="text/tailwindcss">
        @layer utilities {
            .content-auto {
                content-visibility: auto;
            }
            .text-shadow {
                text-shadow: 0 2px 4px rgba(0,0,0,0.1);
            }
        }
        
        /* 移除iframe滚动条 */
        #m3u8-player {
            overflow: hidden !important;
            border: none !important;
            margin: 0 !important;
            padding: 0 !important;
            width: 100% !important;
            height: 100% !important;
            position: absolute !important;
            top: 0 !important;
            left: 0 !important;
        }
        
        /* 确保视频容器正确显示 */
        #video-container {
            position: relative !important;
            width: 100% !important;
            height: 0 !important;
            padding-bottom: 56.25% !important; /* 16:9 宽高比 */
            overflow: hidden !important;
        }

        .toast {
            position: fixed;
            top: 20px;
            right: 20px;
            background-color: rgba(0, 0, 0, 0.8);
            color: white;
            padding: 12px 24px;
            border-radius: 4px;
            z-index: 1000;
            display: none;
            animation: fadeInOut 2.5s ease-in-out;
        }

        @keyframes fadeInOut {
            0% { opacity: 0; }
            10% { opacity: 1; }
            90% { opacity: 1; }
            100% { opacity: 0; }
        }

        .video-actions {
            display: flex;
            gap: 16px;
            margin-top: 16px;
        }

        .action-btn {
            display: flex;
            align-items: center;
            gap: 8px;
            padding: 8px 16px;
            border: none;
            border-radius: 4px;
            background-color: #f0f0f0;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .action-btn:hover {
            background-color: #e0e0e0;
        }

        .action-btn.active {
            background-color: #ff4d4f;
            color: white;
        }

        .action-btn i {
            font-size: 16px;
        }
    </style>
</head>
<body class="bg-gray-50 font-inter text-dark">
    <!-- 导航栏 -->
    <header class="sticky top-0 z-50 bg-white/90 backdrop-blur-sm shadow-sm">
        <div class="container mx-auto px-4 py-3 flex items-center justify-between">
            <!-- logo -->
            <a href="/" class="flex-shrink-0 flex items-center space-x-1 z-10">
                <i class="fa fa-play-circle text-primary text-3xl"></i>
                <span class="text-xl font-bold text-primary">三石影视</span>
            </a>
            
            <nav class="hidden md:flex items-center space-x-8">
                <a href="{{ url_for('index') }}" class="font-medium {% if active_nav == 'home' %}text-primary border-b-2 border-primary pb-1{% else %}text-gray-600 hover:text-primary transition-colors{% endif %}">首页</a>
                <a href="{{ url_for('category_movie') }}" class="font-medium {% if active_nav == 'movie' %}text-primary border-b-2 border-primary pb-1{% else %}text-gray-600 hover:text-primary transition-colors{% endif %}">电影</a>
                <a href="{{ url_for('category_tv') }}" class="font-medium {% if active_nav == 'tv' %}text-primary border-b-2 border-primary pb-1{% else %}text-gray-600 hover:text-primary transition-colors{% endif %}">电视剧</a>
                <a href="{{ url_for('category_anime') }}" class="font-medium {% if active_nav == 'anime' %}text-primary border-b-2 border-primary pb-1{% else %}text-gray-600 hover:text-primary transition-colors{% endif %}">动漫</a>
                <a href="{{ url_for('category_variety') }}" class="font-medium {% if active_nav == 'variety' %}text-primary border-b-2 border-primary pb-1{% else %}text-gray-600 hover:text-primary transition-colors{% endif %}">综艺</a>
            </nav>
            
            <div class="flex items-center space-x-4">
                <div class="relative hidden md:block">
                    <form method="get" action="/">
                        <input type="text" name="q" value="{{ search_query|default('') }}" placeholder="搜索视频..." class="pl-10 pr-4 py-2 rounded-full bg-gray-100 focus:outline-none focus:ring-2 focus:ring-primary/50 w-64">
                        <button type="submit" class="absolute right-3 top-1/2 transform -translate-y-1/2 text-gray-400"><i class="fa fa-search"></i></button>
                        <i class="fa fa-search absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400"></i>
                    </form>
                </div>
                <button class="md:hidden text-gray-600">
                    <i class="fa fa-search text-xl"></i>
                </button>
                {% if session.get('user_id') %}
                <div class="relative">
                    <button id="userMenuBtn" class="text-gray-600 hover:text-primary transition-colors">
                        <i class="fa fa-user-circle text-xl"></i>
                    </button>
                    <div id="userMenu" class="absolute right-0 mt-2 w-48 bg-white rounded-lg shadow-lg py-2 hidden">
                        <a href="{{ url_for('profile') }}" class="block px-4 py-2 text-gray-700 hover:bg-gray-100">
                            <i class="fa fa-user mr-2"></i>个人中心
                        </a>
                        {% if session.get('role') == 'admin' %}
                        <a href="{{ url_for('admin_dashboard') }}" class="block px-4 py-2 text-gray-700 hover:bg-gray-100">
                            <i class="fa fa-cog mr-2"></i>管理控制台
                        </a>
                        {% endif %}
                        <a href="{{ url_for('logout') }}" class="block px-4 py-2 text-gray-700 hover:bg-gray-100">
                            <i class="fa fa-sign-out-alt mr-2"></i>退出登录
                        </a>
                    </div>
                </div>
                {% else %}
                <a href="{{ url_for('login') }}" class="text-gray-600 hover:text-primary transition-colors">
                    <i class="fa fa-user-circle text-xl"></i>
                </a>
                {% endif %}
                <button class="md:hidden text-gray-600">
                    <i class="fa fa-bars text-xl"></i>
                </button>
            </div>
        </div>
    </header>

    <main class="container mx-auto px-4 py-8">
        <!-- 面包屑导航 -->
        <div class="flex items-center text-sm text-gray-500 mb-6">
            <a href="{{ url_for('index') }}" class="hover:text-primary transition-colors">首页</a>
            <i class="fa fa-angle-right mx-2 text-gray-400"></i>
            <span class="text-primary">{{ video.title }}</span>
        </div>
        
        <!-- 视频播放区域 -->
        <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
            <!-- 左侧视频播放器 -->
            <div class="lg:col-span-2">
                <div class="relative bg-black rounded-xl overflow-hidden shadow-lg" style="aspect-ratio: 16/9;">
                    <div id="video-container" class="w-full h-full">
                        {% if video_url.endswith('.m3u8') %}
                            <iframe id="m3u8-player" 
                                class="w-full h-full" 
                                style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border: none; overflow: hidden;"
                                frameborder="0" 
                                scrolling="no"
                                allowfullscreen 
                                src="https://m3u8player.org/player.html?url={{ video_url }}&autoplay=true&loop=false&muted=false&poster={{ video.cover }}">
                            </iframe>
                        {% elif video_url.endswith('.mp4') %}
                            <video id="video-player" class="w-full h-full" controls controlsList="nodownload" poster="{{ video.cover }}" src="{{ video_url }}"></video>
                        {% else %}
                            <iframe id="video-player" 
                                class="w-full h-full" 
                                style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border: none; overflow: hidden;"
                                frameborder="0" 
                                scrolling="no"
                                allowfullscreen 
                                src="{{ video_url }}">
                            </iframe>
                        {% endif %}
                    </div>
                </div>
                
                <!-- 播放信息 -->
                <div class="mt-4 bg-white p-4 rounded-xl shadow-sm">
                    <div class="flex justify-between items-start mb-4">
                        <div>
                            <h2 class="text-xl font-bold mb-2">{{ video.title }}</h2>
                            <div class="flex flex-wrap gap-2">
                                {% for tag in video.tags %}
                                <span class="text-xs bg-gray-100 text-gray-600 px-2 py-1 rounded-full">{{ tag }}</span>
                                {% endfor %}
                            </div>
                        </div>
                        <div class="flex gap-4">
                            <button id="favoriteBtn" class="flex items-center gap-2 px-4 py-2 rounded-lg bg-gray-100 hover:bg-gray-200 transition-colors">
                                <i class="fa fa-heart"></i>
                                <span>收藏</span>
                            </button>
                            <button onclick="shareVideo()" class="flex items-center gap-2 px-4 py-2 rounded-lg bg-gray-100 hover:bg-gray-200 transition-colors">
                                <i class="fa fa-share-alt"></i>
                                <span>分享</span>
                            </button>
                        </div>
                    </div>
                    <p class="text-gray-600 leading-relaxed">{{ video.description }}</p>
                </div>
            </div>
            
            <!-- 右侧视频信息和选集 -->
            <div class="lg:col-span-1">
                <div class="bg-white rounded-xl shadow-sm overflow-hidden">
                    <div class="p-4 border-b border-gray-100">
                        <h3 class="font-semibold text-lg">剧集信息</h3>
                    </div>
                    
                    <div class="p-4">
                        <div class="flex space-x-4 mb-4">
                            <img src="{{ video.cover }}" alt="{{ video.title }}" class="w-24 h-32 object-cover rounded-lg">
                            <div>
                                <h4 class="font-semibold text-lg mb-1">{{ video.title }}</h4>
                                <div class="flex items-center mb-1">
                                    <i class="fa fa-star text-yellow-400"></i>
                                    <span class="ml-1 text-sm font-medium">{{ video.rating }}</span>
                                    <span class="ml-1 text-xs text-gray-500">/ 10</span>
                                </div>
                                <div class="text-sm text-gray-600 space-y-1">
                                    <p><span class="font-medium">主演:</span> {{ video.cast }}</p>
                                    <p><span class="font-medium">导演:</span> {{ video.director }}</p>
                                    <p><span class="font-medium">地区:</span> {{ video.region }}</p>
                                    <p><span class="font-medium">类型:</span> {{ video.category }}</p>
                                </div>
                            </div>
                        </div>
                        
                        <div class="mb-4">
                            <h4 class="font-semibold mb-2">简介</h4>
                            <p class="text-sm text-gray-600 line-clamp-3">{{ video.description }}</p>
                        </div>
                        
                        <!-- 选集 -->
                        <div>
                            <div class="flex items-center justify-between mb-3">
                                <h4 class="font-semibold">选集</h4>
                                <div class="flex space-x-2">
                                    {% if prev_episode %}
                                    <a href="{{ url_for('play', video_id=video.id) }}?episode={{ prev_episode }}" class="text-sm text-primary hover:text-primary/80 transition-colors">
                                        <i class="fa fa-step-backward mr-1"></i>上一集
                                    </a>
                                    {% endif %}
                                    {% if next_episode %}
                                    <a href="{{ url_for('play', video_id=video.id) }}?episode={{ next_episode }}" class="text-sm text-primary hover:text-primary/80 transition-colors">
                                        下一集<i class="fa fa-step-forward ml-1"></i>
                                    </a>
                                    {% endif %}
                                </div>
                            </div>
                            
                            <div class="grid grid-cols-5 gap-2">
                                {% for ep in available_episodes %}
                                <a href="{{ url_for('play', video_id=video.id) }}?episode={{ ep.num }}"
                                   class="text-center py-2 border border-gray-200 rounded-md text-sm hover:border-primary hover:text-primary transition-colors {% if ep.num == episode %}bg-primary/10 border-primary text-primary{% endif %}">
                                    {{ ep.key }}
                                </a>
                                {% endfor %}
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- 猜你喜欢 -->
                <div class="bg-white rounded-xl shadow-sm overflow-hidden mt-6">
                    <div class="p-4 border-b border-gray-100">
                        <h3 class="font-semibold text-lg">猜你喜欢</h3>
                    </div>
                    
                    <div class="p-4">
                        <div class="space-y-4">
                            {% for related_video in videos[:4] if related_video.id != video.id %}
                            <a href="{{ url_for('play', video_id=related_video.id) }}" class="flex space-x-3 group">
                                <img src="{{ related_video.cover }}" alt="{{ related_video.title }}" class="w-20 h-12 object-cover rounded group-hover:opacity-90 transition-opacity">
                                <div>
                                    <h4 class="font-medium text-sm line-clamp-1 group-hover:text-primary transition-colors">{{ related_video.title }}</h4>
                                    <div class="flex items-center text-xs text-gray-500 mt-1">
                                        <span><i class="fa fa-eye mr-1"></i>{{ related_video.views }}万</span>
                                    </div>
                                </div>
                            </a>
                            {% endfor %}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </main>

    <footer class="bg-dark text-white py-10 mt-16">
        <div class="container mx-auto px-4">
            <div class="grid grid-cols-1 md:grid-cols-4 gap-8">
                <div>
                    <h3 class="text-xl font-bold mb-4">三石影视</h3>
                    <p class="text-gray-400">提供高质量视频播放服务，享受流畅观影体验</p>
                </div>
                <div>
                    <h4 class="font-semibold mb-4">快速链接</h4>
                    <ul class="space-y-2">
                        <li><a href="{{ url_for('index') }}" class="text-gray-400 hover:text-white transition-colors">首页</a></li>
                        <li><a href="{{ url_for('category_movie') }}" class="text-gray-400 hover:text-white transition-colors">电影</a></li>
                        <li><a href="{{ url_for('category_tv') }}" class="text-gray-400 hover:text-white transition-colors">电视剧</a></li>
                        <li><a href="{{ url_for('category_anime') }}" class="text-gray-400 hover:text-white transition-colors">动漫</a></li>
                        <li><a href="{{ url_for('category_variety') }}" class="text-gray-400 hover:text-white transition-colors">综艺</a></li>
                    </ul>
                </div>
                <div>
                    <h4 class="font-semibold mb-4">帮助中心</h4>
                    <ul class="space-y-2">
                        <li><a href="#" class="text-gray-400 hover:text-white transition-colors">常见问题</a></li>
                        <li><a href="#" class="text-gray-400 hover:text-white transition-colors">使用指南</a></li>
                        <li><a href="#" class="text-gray-400 hover:text-white transition-colors">联系我们</a></li>
                    </ul>
                </div>
                <div>
                    <h4 class="font-semibold mb-4">关注我们</h4>
                    <div class="flex space-x-4">
                        <a href="#" class="text-gray-400 hover:text-white transition-colors">
                            <i class="fa fa-weibo text-xl"></i>
                        </a>
                        <a href="#" class="text-gray-400 hover:text-white transition-colors">
                            <i class="fa fa-wechat text-xl"></i>
                        </a>
                        <a href="#" class="text-gray-400 hover:text-white transition-colors">
                            <i class="fa fa-qq text-xl"></i>
                        </a>
                    </div>
                </div>
            </div>
            <div class="border-t border-gray-700 mt-8 pt-8 text-center text-gray-500 text-sm">
                <p>© 2025 三石影视. 保留所有权利</p>
            </div>
        </div>
    </footer>

    <!-- 添加提示框 -->
    <div id="toast" class="toast"></div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 显示提示框
            function showToast(message) {
                const toast = document.getElementById('toast');
                if (!toast) return;
                
                toast.textContent = message;
                toast.style.display = 'block';
                setTimeout(() => {
                    toast.style.display = 'none';
                }, 2500);
            }

            // 检查是否已收藏
            async function checkFavorite() {
                try {
                    const formData = new URLSearchParams();
                    formData.append('video_id', '{{ video.id }}');
                    
                    const response = await fetch('/check_favorite', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/x-www-form-urlencoded',
                        },
                        body: formData
                    });
                    
                    if (!response.ok) {
                        throw new Error(`HTTP error! status: ${response.status}`);
                    }
                    
                    const data = await response.json();
                    if (data.success) {
                        const favoriteBtn = document.getElementById('favoriteBtn');
                        if (favoriteBtn) {
                            const span = favoriteBtn.querySelector('span');
                            if (span) {
                                if (data.is_favorite) {
                                    favoriteBtn.classList.add('active');
                                    span.textContent = '已收藏';
                                } else {
                                    favoriteBtn.classList.remove('active');
                                    span.textContent = '收藏';
                                }
                            }
                        }
                    } else {
                        console.error('检查收藏状态失败:', data.message);
                        showToast(data.message || '检查收藏状态失败');
                    }
                } catch (error) {
                    console.error('检查收藏状态失败:', error);
                    showToast('检查收藏状态失败');
                }
            }

            // 收藏按钮点击事件
            const favoriteBtn = document.getElementById('favoriteBtn');
            if (favoriteBtn) {
                favoriteBtn.addEventListener('click', async function() {
                    try {
                        const formData = new URLSearchParams();
                        formData.append('video_id', '{{ video.id }}');
                        
                        const response = await fetch('/add_favorite', {
                            method: 'POST',
                            headers: {
                                'Content-Type': 'application/x-www-form-urlencoded',
                            },
                            body: formData
                        });
                        
                        if (!response.ok) {
                            throw new Error(`HTTP error! status: ${response.status}`);
                        }
                        
                        const data = await response.json();
                        if (data.success) {
                            const span = this.querySelector('span');
                            if (span) {
                                if (data.action === 'added') {
                                    this.classList.add('active');
                                    span.textContent = '已收藏';
                                    showToast('收藏成功');
                                } else {
                                    this.classList.remove('active');
                                    span.textContent = '收藏';
                                    showToast('已取消收藏');
                                }
                            }
                        } else {
                            showToast(data.message || '操作失败');
                        }
                    } catch (error) {
                        console.error('收藏操作失败:', error);
                        showToast('操作失败，请稍后重试');
                    }
                });
            }

            // 分享视频
            window.shareVideo = function() {
                try {
                    const url = window.location.href;
                    if (navigator.share) {
                        navigator.share({
                            title: '{{ video.title }}',
                            text: '{{ video.description }}',
                            url: url
                        }).catch(error => {
                            console.error('分享失败:', error);
                            showToast('分享失败');
                        });
                    } else {
                        navigator.clipboard.writeText(url).then(() => {
                            showToast('链接已复制到剪贴板');
                        }).catch(() => {
                            showToast('复制链接失败');
                        });
                    }
                } catch (error) {
                    console.error('分享操作失败:', error);
                    showToast('分享失败');
                }
            }

            // 页面加载时检查收藏状态
            checkFavorite();

            // 视频开始播放时记录历史
            const video = document.querySelector('video');
            if (video) {
                video.addEventListener('play', function() {
                    recordHistory();
                });
            }

            // 记录观看历史
            async function recordHistory() {
                try {
                    const response = await fetch('/add_history', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/x-www-form-urlencoded',
                        },
                        body: `video_id={{ video.id }}`
                    });
                    const data = await response.json();
                    if (!data.success) {
                        console.error('记录观看历史失败:', data.message);
                    }
                } catch (error) {
                    console.error('记录观看历史失败:', error);
                }
            }

            // 用户菜单显示/隐藏
            const userMenuBtn = document.getElementById('userMenuBtn');
            const userMenu = document.getElementById('userMenu');
            
            if (userMenuBtn && userMenu) {
                userMenuBtn.addEventListener('click', function(e) {
                    e.stopPropagation();
                    userMenu.classList.toggle('hidden');
                });
                
                // 点击其他地方关闭菜单
                document.addEventListener('click', function(e) {
                    if (!userMenu.contains(e.target) && !userMenuBtn.contains(e.target)) {
                        userMenu.classList.add('hidden');
                    }
                });
            }
        });
    </script>
</body>
</html>    